<?php
/**
 * 计划管理
*/
apf_require_class("Bll_BLL");

class Bll_Jx_Setting extends BLL {

    /**
     * 根据一个月份计算属于那年的那次考核
     */
    public function format_performance(){
        $last = $this->get_last_performance(0);
        $today = date('m');
        $two = array();
        foreach($last as $k=>$val){
            if($today>=$val['s_month'] && $today <=$val['e_month']){
                if(!empty($last[$k-1])){
                    $two[] = $this->_format_p_no($last[$k-1]);
                }else{
                    $two[] = array();
                }
                $two[] = $this->_format_p_no($val);
                break;
            }
        }
        return $two;
    }
    private function _format_p_no($val){
        return $val['year'].($val['s_month'] < 10 ? '0'.$val['s_month'] :$val['s_month']).($val['e_month'] < 10 ? '0'.$val['e_month'] :$val['e_month']);
    }
    public function set_check_month(){

    }
    public function set_check_day($data){
        $_where = array(
                'revicwMonth'=>$data['revicwMonth'],
                'step_status'=>$data['step_status'],
                'step_date'=>$data['step_date'],
                );
        $ck_data = $this->get_day_dao()->find($_where);
        if($ck_data){
            $this->get_day_dao()->update($data,array('id'=>$ck_data['id']));
        }else{
            $this->get_day_dao()->insert($data);
        }
        return true;
    }
    //写入review day
    public function insert_review_month($data){
        return $this->get_month_dao()->insert($data);
    }
    public function remove_review_month($year,$last_month=0){
        $_where = array(
                "year"=>$year,
        );
        if($last_month){
               $_where["month > $last_month AND 1=?"]=1;
        }
        return $this->et_month_dao()->remove($_where);
    }
    /**
     * 获取当年考评月
     */
    public function get_performance_month_sort($year){
        /*$_s = array();
        for($i=1;$i<13;$i++){
            $_s[] = $year.($i<10 ? '0'.$i : $i);
        }
        $_where = array(
                "revicwMonth IN(".implode(',',$_s).") AND 1=?"=>1,
                "step_status"=>1
                );
        $list = $this->get_day_dao()->find($_where);
        $day_list = array();
        foreach($list as $val){
            $day_list[$val['step_date']] = $val;
        }
        return $day_list;*/
    }
    /**
     * 取最后两次的考评,包括这次
     */
    public function get_last_performance($limit =2){
        if($limit){
            $today = date('Ymd');
            $_where = array(
                    "reviewDay <= ".$today." AND 1=?" =>1
            );
        }
        return $this->get_month_dao()->find($_where,'id ASC' ,$limit);
    }
    /**
     * 获取某次的考评日期
     * @param int $year 201304
     */
    public function get_performance_days($year){
        return $this->get_day_dao()->find(array('revicwMonth'=>$year));
    }
    /**
     * 得到一年的考评月
     * @param int $year
     */
    public function get_performance_month($year=0){
        $_where = array();
        if($year){
            $_where['year'] = $year;
        }
        $data = $this->get_month_dao()->find($_where,'id ASC');
        $last = array();
        if(!empty($data)){
            foreach($data as $val){
                $last[] =$this->_format_p_no($val);
            }
        }
        return $last;
    }
    /**
     * 得到系统中的考评年
     */
    public function get_performance_year($year){
        return $this->get_month_dao()->find(array('year'=>$year),'id ASC');
    }
    /**
     * 获取当前次考评的数据
     */
    public function get_current_check(){
        $date = date('Ym');
        return $this->get_day_dao()->find(array('revicwMonth'=>$date,'step_status'=>3));
    }
    //设置考评月
    private function get_month_dao (){
        $dao = $this->get_dao('Dao_jx_CheckMonth');
        return $dao;
    }
    //设置每次考评日期
    private function get_day_dao (){
        $dao = $this->get_dao('Dao_jx_CheckDate');
        return $dao;
    }
    public function get_user_code ($user_id){
        return $this->get_use_dao()->find_row(array('user_id'=>$user_id));
    }
    public function get_adjustment_logs($userCode){
        return $this->get_current_dao()->find(array('userCode'=>$userCode));
    }
    public function get_current_dao(){
        return $this->get_dao("Dao_jx_ModifyLog");
    }
    public function get_use_dao(){
        return $this->get_dao("Dao_jx_UserKp");
    }
    public function get_virtual_dao(){
        return $this->get_dao("Dao_jx_VirtualLeader");
    }
    public function get_user_info_dao(){
        return $this->get_dao("Dao_jx_UserInfo");
    }
    public function get_underling_info($superior_code){
        return $this->get_use_dao()->find(array('superior_code'=>$superior_code));
    }
    public function set_superior($ids,$superior_code){
        //$ids是工号 eg: SHE2687,SHD724 ...,$superior_code是上级工号
        if($ids && $superior_code)
        foreach ($ids as $id){
        $this->get_use_dao()->update(array('superior_code'=>$superior_code),array('code'=>$id));
        }
        return true;
    }
    public function get_superior_id($user_id){
        //$user_id是工号
        return $this->get_use_dao()->find_row(array("code"=>$user_id));
    }
    public function get_member_by_code($user_code){
        return $this->get_use_dao()->find(array('code'=>$user_code ));
    }
    public function set_change_member_superior($superior_code,$underling_code){
        return $this->get_use_dao()->update(array('superior_code'=>$superior_code),array('code'=>$underling_code));
    }
    public function get_member_superior_code($superior_code){
        return $this->get_use_dao()->find(array('superior_code'=>$superior_code));
    }
    public function insert_into_kp_table($user_id,$name,$code,$superior_code,$enter_date,$department_id){
        $this->get_use_dao()->insert(array("user_id"=>$user_id,"name"=>$name,"code"=>$code,
        "superior_code"=>$superior_code,"enter_date"=>$enter_date,"department_id"=>$department_id));
    }
    public function get_user_info_by_name($name){
        return $this->get_use_dao()->find(array("name"=>$name));
    }
    public function set_virtual_leader($leaderId,$userId ,$code ){
        return $this->get_virtual_dao()->insert(array('leaderId'=>$leaderId,'userId'=>$userId,'code'=>$code));
    }
    public function get_user_info($name){
        return $this->get_user_info_dao()->find(array('name'=>$name,'job_status'=>'在职'));
    }
    public function get_user_info_by_id($user_id){
        return $this->get_user_info_dao()->find(array('user_id'=>$user_id,'job_status'=>'在职'));
    }
}